﻿Imports Autodesk.AutoCAD.ApplicationServices
Imports System.IO
Imports System.Xml.Serialization
Public Class RCMS_Drawing_Properties
    <Xml.Serialization.XmlIgnore()> Public v_file As String
    <Xml.Serialization.XmlIgnore()> Public v_projectname As String
    <Xml.Serialization.XmlIgnore()> Public v_dosave As String
    <Xml.Serialization.XmlIgnore()> Public v_source As String
    <Xml.Serialization.XmlIgnore()> Public v_passwd As String
    <Xml.Serialization.XmlIgnore()> Public v_prpass As String
    Public v_url As String
    Public v_userid As String
    Public v_projid As String
    Public v_powner As String
    Public x_passwd As String
    Public v_appl As String
    Public v_focus As String
    Public v_pruser As String
    Public x_prpass As String
    Public Sub SaveProperties(Optional ByVal p_path As String = "")
        Dim v_path As String
        If p_path = "" Then
            v_path = v_file
        Else
            v_path = p_path
        End If
        If v_path = "" Then
            Exit Sub
        End If
        If File.Exists(v_path) Then
            Dim d As String = "yyyyMMdd_HHmmss"
            Dim n As DateTime = DateTime.Now
            d = n.ToString(d)
            Dim b As String = v_path
            b = Path.GetFileNameWithoutExtension(b)
            b = b & "_" & d & ".xml"
            My.Computer.FileSystem.RenameFile(v_path, b)
        End If
        If v_passwd = "" Then
            x_passwd = ""
        Else
            x_passwd = CLDS_Static_Utils.CLDS_Encrypt(v_passwd)
        End If
        If v_pruser = "" Or v_prpass = "" Then
            x_prpass = ""
        Else
            x_prpass = CLDS_Static_Utils.CLDS_Encrypt(v_prpass)
        End If
        Dim objSerial As XmlSerializer
        Dim objStream As FileStream
        objStream = New FileStream(v_path, FileMode.OpenOrCreate)
        objSerial = New XmlSerializer(Me.GetType)
        objSerial.Serialize(objStream, Me)
        objStream.Close()
    End Sub

    Public Function LoadProperties(Optional ByVal p_path As String = "") As Object
        Dim v_path As String
        If p_path = "" Then
            v_path = v_file
        Else
            v_path = p_path
        End If
        Dim objSerial As XmlSerializer
        Dim objStream As FileStream
        v_projectname = ""
        Dim x As Boolean = Microsoft.VisualBasic.FileIO.FileSystem.FileExists(v_path)
        If Not x Then
            v_path = CLDS_Static_Utils.CLDS_FindFile("RCMS_Defaults.xml")
            x = Microsoft.VisualBasic.FileIO.FileSystem.FileExists(v_path)
            If x Then
                v_projectname = Application.GetSystemVariable("PROJECTNAME")
            Else
                MsgBox("Unable to load properties from XML file" & vbLf & v_path, MsgBoxStyle.Critical, "RCMS AutoCAD")
                LoadProperties = Nothing
                Exit Function
            End If
        End If
        v_file = v_path
        objStream = New FileStream(v_path, FileMode.Open)
        objSerial = New XmlSerializer(Me.GetType)
        'Dim loaded As CLDS_Drawing_Properties = DirectCast(objSerial.Deserialize(objStream), CLDS_Drawing_Properties)
        'p_loaded = DirectCast(objSerial.Deserialize(objStream), p_loaded.gettype)
        Dim p_loaded As Object = objSerial.Deserialize(objStream)
        objStream.Close()
        v_dosave = "N"
        If p_loaded.x_passwd = "" Then
            p_loaded.v_passwd = ""
        Else
            Dim lb As Integer = InStr(p_loaded.x_passwd, "[", CompareMethod.Text)
            Dim rb As Integer = InStr(p_loaded.x_passwd, "]", CompareMethod.Text)
            If lb > 0 And rb > lb Then
                p_loaded.v_passwd = Mid$(p_loaded.x_passwd, lb + 1, rb - lb - 1)
                v_dosave = "Y"
            Else
                p_loaded.v_passwd = CLDS_Static_Utils.CLDS_Decrypt(p_loaded.x_passwd)
            End If
        End If
        If p_loaded.x_prpass = "" Then
            p_loaded.v_prpass = ""
        Else
            Dim lb As Integer = InStr(p_loaded.x_prpass, "[", CompareMethod.Text)
            Dim rb As Integer = InStr(p_loaded.x_prpass, "]", CompareMethod.Text)
            If lb > 0 And rb > lb Then
                p_loaded.v_prpass = Mid$(p_loaded.x_prpass, lb + 1, rb - lb - 1)
                v_dosave = "Y"
            Else
                p_loaded.v_prpass = CLDS_Static_Utils.CLDS_Decrypt(p_loaded.x_prpass)
            End If
        End If
        If p_loaded.v_projid <> "" Then
            Dim lb As Integer = InStr(p_loaded.v_projid, "[", CompareMethod.Text)
            Dim rb As Integer = InStr(p_loaded.v_projid, "]", CompareMethod.Text)
            If lb > 0 And rb > lb Then
                Dim pname As String = Mid$(p_loaded.v_projid, lb + 1, rb - lb - 1)
                pname = Application.GetSystemVariable(UCase(pname))
                If pname = "" Then
                    v_dosave = "N"
                Else
                    p_loaded.v_projid = pname
                    v_dosave = "Y"
                End If
            End If
        End If
        Me.v_url = p_loaded.v_url
        Me.v_userid = p_loaded.v_userid
        Me.v_passwd = p_loaded.v_passwd
        Me.v_projid = p_loaded.v_projid
        Me.v_powner = p_loaded.v_powner
        Me.v_appl = p_loaded.v_appl
        Me.v_focus = p_loaded.v_focus
        Me.v_source = p_loaded.v_source
        Me.v_pruser = p_loaded.v_pruser
        Me.v_prpass = p_loaded.v_prpass
        Me.v_source = CLDS_Source_From_File(Microsoft.VisualBasic.FileIO.FileSystem.GetName(Application.DocumentManager.MdiActiveDocument.Name))
        If v_dosave = "Y" Then
            Me.SaveProperties()
        End If
        LoadProperties = p_loaded
    End Function
End Class
